package org.third.security.crypto;

import java.util.UUID;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.dao.ReflectionSaltSource;
import org.springframework.security.authentication.dao.SystemWideSaltSource;
import org.springframework.security.authentication.encoding.LdapShaPasswordEncoder;
import org.springframework.security.authentication.encoding.PlaintextPasswordEncoder;

public class PasswordTest {

	static final Logger logger = LoggerFactory.getLogger(PasswordTest.class);

	@Test
	public void testSpring_PlaintextPasswordEncoder() {
		PlaintextPasswordEncoder encoder = new PlaintextPasswordEncoder();
		SystemWideSaltSource salt = new SystemWideSaltSource();
		salt.setSystemWideSalt("@$X");
		// salt.setUserPropertyToUse(user)
		String rawPass = "Test1";
		String str = encoder.encodePassword(rawPass, salt);
		System.out.println(str);

		boolean str1 = encoder.isPasswordValid(str, rawPass, new ReflectionSaltSource());
		System.out.println(str1);

	}

	@Test
	public void testLdapShaPasswordEncoder() {
		LdapShaPasswordEncoder e = new LdapShaPasswordEncoder();
		String rawPass = "Test1";
		String str = e.encodePassword(rawPass, UUID.randomUUID().toString().getBytes());
		System.out.println(str);

		boolean str1 = e.isPasswordValid(str, rawPass, UUID.randomUUID().toString().getBytes());
		System.out.println(str1);
	}

	public static void main(String[] args) {
//        int maxLoop = 1_000_000;
//        long start = System.currentTimeMillis();
//        for (int i = 0; i < maxLoop; i++) {
//            Thread.currentThread();
//            StackTraceElement e = Thread.currentThread().getStackTrace()[1];
//        }
//        long end = System.currentTimeMillis();
//        System.out.println(end - start);
//
//        start = System.currentTimeMillis();
//        for (int i = 0; i < maxLoop; i++) {
//            int x = 0;
//        }
//        end = System.currentTimeMillis();
//        System.out.println(end - start);
//        
//        
//        start = System.currentTimeMillis();
//        for (int i = 0; i < maxLoop; i++) {
//            Map<String, String> m  = new HashMap<String, String>();
//        }
//        end = System.currentTimeMillis();
//        System.out.println(end - start);
		String password = "test";
		System.out.println(SecurityHelper.encrypt(password));
		System.out.println(SecurityHelper.encrypt(password));

		System.out.println(SecurityHelper.hash(password));
		System.out.println(SecurityHelper.hash(password));
	}
}
